package com.easygoods.shiro.realm;

import com.bestvike.stone.core.lang.RandomUtils;
import com.bestvike.stone.spring.shiro.realm.Realm;
import com.easygoods.dao.MemberLoginDao;
import com.easygoods.entity.MemberLogin;
import com.easygoods.enums.Channel;
import com.easygoods.enums.LoginResult;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by 许崇雷 on 2019-03-12.
 */
public abstract class AbstractRealm implements Realm {
    @Autowired
    private MemberLoginDao memberLoginDao;

    //记录登录日志
    protected void writeLoginLog(String memberId, String mobile, Channel channel, String openId, LoginResult result) {
        MemberLogin loginInfo = new MemberLogin();
        loginInfo.setId(RandomUtils.randomUUID());
        loginInfo.setMemberId(memberId);
        loginInfo.setMobile(mobile);
        loginInfo.setExternalChannel(channel == null ? null : channel.name());
        loginInfo.setExternalId(openId);
        loginInfo.setResult(result.name());
        loginInfo.beforeInsert();
        this.memberLoginDao.insert(loginInfo);
    }
}
